package com.ymatou.api.sellerpromotion.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;
import com.ymatou.api.sellerpromotion.service.GetSellerProductPromotionInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;

/**
 * @serial 2016-12-25
 * @author chencan
 * 计算满减金额
 */
public class Ts_GetSellerProductPromotionInfo {
	private static GetSellerProductPromotionInfoCall getSellerProductPromotionInfoCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("业务说明：计算促销优惠金额");
	}

	@Before
	public void caseUp() {
		getSellerProductPromotionInfoCall = new GetSellerProductPromotionInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerProductPromotionInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductPromotionInfo_001() {
		Logger.start(true, "计算促销优惠");
		try {
			int sellerid = 20354944;
			String catalog = "4aa0f22c-ad51-409b-a1ae-c63208794204";
			
			ArrayList<Map> mongoDataMap = new SellerPromotionInfoWapper().getPromotions(sellerid);
			Map map = mongoDataMap.get(0);
			BasicDBList productIds = (BasicDBList) map.get("ProductIds");
			String productId = (String) productIds.get(1);
			BasicDBList promotionValues = (BasicDBList) map.get("PromotionValues");
			BasicDBObject first = (BasicDBObject) promotionValues.get(0);
			int productUnitPrice = first.getInt("QtyCondition");
			String productQty = first.getString("Value");
			
			
			Map<String,Object> updateJsonMap = new HashMap<>();
			updateJsonMap.put("SellerProductInfos[0]->ProductInfos[0]->ProductId", productId);
			
			
			JSONObject retBea = new GetSellerProductPromotionInfoCall().callAndGetReturnData(updateJsonMap);
			JSONObject data = retBea.getJSONObject("Data").getJSONArray("SellerPromotionInfos").getJSONObject(0);
			JSONObject promotionInfos = data.getJSONArray("PromotionInfos").getJSONObject(0);
			JSONObject policyInfo = promotionInfos.getJSONObject("PromotionPolicyInfo");
			JSONObject values = policyInfo.getJSONArray("PromotionValues").getJSONObject(0);
			JSONObject productInfos = promotionInfos.getJSONArray("ProductInfos").getJSONObject(0);
			
			int code = retBea.getInt("Code");
			int seller = data.getInt("SellerId");
			int amount = promotionInfos.getInt("PromotionAmount");
			String product = productInfos.getString("ProductId");
			int unitPrice = productInfos.getInt("ProductUnitPrice");
			int qty = productInfos.getInt("ProductQty");
			int totalprice = productInfos.getInt("ProductTotalPrice");
			int value = productInfos.getInt("PromotionValue");
			int qtycondi = values.getInt("QtyCondition");
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals(sellerid, seller, "买手ID");
			Logger.verifyEquals(productId, product, "商品ID");
			Logger.verifyEquals(10, amount, "满减总金额");
			Logger.verifyEquals(20, unitPrice, "商品单价");
			Logger.verifyEquals(10, qty, "购买数量");
			Logger.verifyEquals(200, totalprice, "商品总金额");
			Logger.verifyEquals(productQty, value, "满减优惠金额");
			Logger.verifyEquals(productUnitPrice, qtycondi, "满件减");
			Logger.verifyNotNull(data, "促销信息不为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductPromotionInfo_002() {
		Logger.start(true, "计算促销优惠--商品ID不存在");
		try {
			String productId = "5f286c80-901b-4e75-b658-ac9d7f2d3333";
			
			Map<String,Object> updateJsonMap = new HashMap<>();
			updateJsonMap.put("SellerProductInfos[0]->ProductInfos[0]->ProductId", productId);
			
			
			JSONObject retBea = new GetSellerProductPromotionInfoCall().callAndGetReturnData(updateJsonMap);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			String promotioninfos = data.getString("SellerPromotionInfos");
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals("[]", promotioninfos, "商品不存在，计算内容为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductPromotionInfo_003() {
		Logger.start(true, "计算促销优惠--买手与商品不符");
		try {
			int sellerid = 2035494400;
			
			Map<String,Object> updateJsonMap = new HashMap<>();
			updateJsonMap.put("SellerProductInfos[0]->SellerId", sellerid);
			
			
			JSONObject retBea = new GetSellerProductPromotionInfoCall().callAndGetReturnData(updateJsonMap);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			String promotioninfos = data.getString("SellerPromotionInfos");
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyIsNull(promotioninfos, "商品不存在，计算内容为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductPromotionInfo_004() {
		Logger.start(true, "计算促销优惠--购买数量不满足促销活动");
		try {
			int productQty = 1;
			
			Map<String,Object> updateJsonMap = new HashMap<>();
			updateJsonMap.put("SellerProductInfos[0]->ProductInfos[0]->ProductQty", productQty);
			
			
			JSONObject retBea = new GetSellerProductPromotionInfoCall().callAndGetReturnData(updateJsonMap);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			String promotioninfos = data.getString("SellerPromotionInfos");
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals("[]", promotioninfos, "商品不存在，计算内容为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerProductPromotionInfo_005() {
		Logger.start(true, "计算促销优惠--商品单价不满足促销活动");
		try {
			int productUnitPrice = 1;
			int sellerid = 20225311;
			
			Map<String,Object> updateJsonMap = new HashMap<>();
			updateJsonMap.put("SellerProductInfos[0]->SellerId", sellerid);
			updateJsonMap.put("SellerProductInfos[0]->ProductInfos[0]->ProductUnitPrice", productUnitPrice);
			
			
			JSONObject retBea = new GetSellerProductPromotionInfoCall().callAndGetReturnData(updateJsonMap);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			String promotioninfos = data.getString("SellerPromotionInfos");
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals("null", promotioninfos, "商品不存在，计算内容为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
